package cn.bdqn.controller;

import cn.bdqn.entity.SysUser;
import cn.bdqn.service.SysUserService;
import cn.bdqn.util.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@RestController
public class LoginController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @PostMapping("/sysUser/login")
    public Object login(@RequestBody SysUser sysUser) {

        String code =stringRedisTemplate.opsForValue().get("code");

        if (sysUser.getCode() != null && code.equals(sysUser.getCode())) {
            SysUser sysUser1=sysUserService.login(sysUser.getUsername(),sysUser.getPassword());
                if(sysUser1!=null){
                    return Result.success("200","登录成功！",sysUser1);
                }else{
                    return Result.fail("201","用户名或密码错误！");
                }
        }else{
            return Result.fail("201","验证码输入错误！");
        }
    }
}
